package com.acwing.lc.part1;

import java.util.Stack;

/**
 * @author rkc
 * @date 2022/9/10 10:36
 */
public class LC20有效的括号 {

    private Stack<Character> stk = new Stack<>();

    public boolean isValid(String s) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                stk.add(c);
            } else {
                if (stk.isEmpty()) return false;
                if ((c == ')' && stk.peek() == '(') || (c == ']' && stk.peek() == '[') || (c == '}' && stk.peek() == '{')) {
                    stk.pop();
                } else {
                    return false;
                }
            }
        }
        return stk.isEmpty();
    }
}
